<template>
    <div>
        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>
        <div style="margin: 10px 0">
            <el-upload
                    :action="'http://' + serverIp +':8083/file/upload'"
                    :show-file-list="false"
                    :on-success="handleFileUploadSuccess" style="display: inline-block">
                <el-button type="primary" class="ml-5">上传文件 <i class="el-icon-top"></i></el-button>
            </el-upload>
            <el-popconfirm
                    class="ml-5"
                    confirm-button-text='确定'
                    cancel-button-text='我再想想'
                    icon="el-icon-info"
                    icon-color="red"
                    title="您确定批量删除这些数据吗？"
                    @confirm="delBatch"
            >
                <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>

        </div>
        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                  @selection-change="handleSelectionChange" size="medium">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column
                    prop="articleCover"
                    label="文章封面"
                    width="100"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-image
                            class="article-cover"
                            :src="
              scope.row.img
                ? scope.row.img
                : 'https://static.talkxj.com/articles/c5cc2b2561bd0e3060a500198a4ad37d.png'
            "
                    />

                </template>
            </el-table-column>
            <el-table-column prop="name" label="文件名称"></el-table-column>
            <el-table-column prop="type" label="文件类型"></el-table-column>
            <el-table-column prop="size" label="文件大小(kb)"></el-table-column>
            <el-table-column prop="points" label="下载积分"></el-table-column>
            <el-table-column label="预览">
                <template slot-scope="scope">
                    <el-button type="primary" @click="preview(scope.row.url)">预览</el-button>
                </template>
            </el-table-column>
            <el-table-column label="下载">
                <template slot-scope="scope">
                    <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
                </template>
            </el-table-column>
            <el-table-column label="启用">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ccc"
                               @change="changeEnable(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200" align="center">

                <template slot-scope="scope">

                    <el-button type="success" @click="edit(scope.row)">编辑 <i class="el-icon-edit"></i>
                    </el-button>
                    <el-popconfirm
                            class="ml-5"
                            confirm-button-text='确定'
                            cancel-button-text='我再想想'
                            icon="el-icon-info"
                            icon-color="red"
                            title="您确定删除吗？"
                            @confirm="del(scope.row.id)"
                    >
                        <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[2, 5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!--弹窗-->

        <el-dialog
                title="文件信息"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form :model="form" label-width="120px">
                <el-form-item label="文件名称">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="文件类型">
                    <el-input v-model="form.type" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="文件大小(KB)">
                    <el-input disabled v-model="form.size" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="下载积分">
                    <el-input v-model="form.points" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="icon图标">
                    <el-select v-model="form.img" placeholder="请选择">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>

        </el-dialog>


    </div>
</template>

<script>
    import request from '../utils/request'
    import {serverIp} from "../utils/config";

    export default {
        name: "File",
        data() {
            return {
                serverIp: serverIp,
                options: [{
                    value: 'https://img0.baidu.com/it/u=3589379711,462970278&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    label: 'zip'
                }, {
                    value: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.lenovomm.com%2Fs3%2Fimg%2Ficon%2Fapp%2Fapp-img-lestore%2F5357-2015-11-06012103-1446816063937.png&refer=http%3A%2F%2Fimg.lenovomm.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656032211&t=6d286b63f319ac3c62e9b5ae8fbe15d8',
                    label: 'md'
                }, {
                    value: 'https://img0.baidu.com/it/u=2082831991,1427483447&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    label: 'png'
                }, /* {
                    value: 'https://img0.baidu.com/it/u=2082831991,1427483447&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                    label: 'jpg'
                }, */

                    {
                        value: 'https://img1.baidu.com/it/u=3704756048,334657645&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                        label: 'mp4'
                    },
                    {
                        value: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ffiles.jz5u.com%2Fimgfile%2F20210510%2F10113224dcut.png&refer=http%3A%2F%2Ffiles.jz5u.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656032392&t=e306e5353d1b07ae689fc1ef1dc28052',
                        label: 'pdf'
                    },
                    {
                        value: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.121down.com%2Fattachment%2Fsoft%2F2015%2F0906%2F201430_85170285.png&refer=http%3A%2F%2Fwww.121down.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656032242&t=3cc7813ac5d070bc3d419de7547c4dc2',
                        label: 'exe'
                    },
                    {
                        value: 'https://img0.baidu.com/it/u=3299610380,2152009869&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                        label: 'txt'
                    }],
                form: {},
                dialogVisible: false,
                tableData: [],
                name: '',
                multipleSelection: [],
                pageNum: 1,
                pageSize: 5,
                total: 0
            }
        },
        created() {
            this.load()
        },
        methods: {
            //更新
            save() {
                request.post('/file/update', this.form).then(res => {
                    if (res.code === 20000) {
                        this.$toast({message: '更新成功', type: "success"})
                        this.dialogVisible = false
                    } else {
                        this.$toast({message: '更新失败', type: "success"})
                    }
                })
            },

//编辑
            edit(row) {

                this.form = row
                this.dialogVisible = true
            },

            load() {
                request.get("/file/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.name,
                    }
                }).then(res => {

                    this.tableData = res.data.records
                    this.total = res.data.total

                })
            },
            changeEnable(row) {

                request.post("/file/update", row).then(res => {
                    if (res.code === 20000) {
                        this.$toast({message: '操作成功', type: "success"})
                    }
                })
            },
            del(id) {
                request.delete("/file/" + id).then(res => {
                    if (res.code === 20000) {
                        this.$toast({message: '删除成功', type: "success"})
                        this.load()
                    } else {
                        this.$toast({message: '删除失败', type: "error"})
                    }
                })
            },
            handleSelectionChange(val) {

                this.multipleSelection = val

            },
            delBatch() {
                let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
                request.post("/file/del/batch", ids).then(res => {
                    if (res.code === 20000) {
                        this.$toast({message: '批量删除成功', type: "success"})
                        this.load()
                    } else {
                        this.$toast({message: '批量删除失败', type: "error"})
                    }
                })
            },
            reset() {
                this.name = ""
                this.load()
            },
            handleSizeChange(pageSize) {
                console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                console.log(pageNum)
                this.pageNum = pageNum
                this.load()
            },
            handleFileUploadSuccess(res) {
                console.log(res)
                this.$toast({message: '上传成功', type: "success"})
                this.load()
            },
            download(url) {
                window.open(url)
            },
            preview(url) {
                window.open('https://file.keking.cn/onlinePreview?url=' + encodeURIComponent(window.btoa((url))))
            },
        }
    }
</script>

<style scoped>

</style>
